/**
 * Created by mar105 on 2017-11-14.
 */
import { message } from 'antd';
import * as services from '../services/services';
import * as utils from '../utils/utils';

export default {
  namespace: 'content',
  state: {
    dataList: { sDataListTabId: '' }, /* 配置 */
  },
  reducers: {
    setDataList(state, { payload: { dataList } }) {
      return { ...state, dataList };
    },
  },
  effects: {
    /**   路由跳转   */
    *onRouter({ payload }, { put, call, select }) {
      /*   待用数据赋值   */
      const {
        refresh, /*   刷新方法   */
        url, /*   接口地址   */
        copyTo, /*   复制到数据   */
        checkedId, /*   单据主表编号   */
        sParentId, /*   父页面编号   */
        sFilterOrderBy, /*   列表页排序规则(用于上一条下一条查询)   */
        pages, /*   列表页分页信息(用于上一条下一条查询)   */
        bFilter, /*   列表页查询条件(用于上一条下一条查询)   */
        currentIndex, /*   列表页游标(用于上一条下一条查询)   */
        sListFormmasterId,
      } = payload; /*   参数接收   */
      const token = yield select(state => state.app.token); /*   用户令牌   */
      /*   从接口获取数据   */
      const { data } = yield call(services.getService, token, url);
      /*   判断获取数据成功还是失败   */
      if (data.code === 1) { /*   成功   */
        /*   待用数据赋值   */
        const { rows } = data.dataset; /*   数据集   */
        const currentData = utils.isEmptyArr(rows) ? {} : rows[0]; /*   窗体数据   */
        const paneKey = utils.createNewTabId(); /*   页签标识   */
        /*   添加页签   */
        yield put({
          type: 'app/addNewPane',
          payload: {
            pane: {
              refresh, /*   刷新方法   */
              copyTo, /*   复制到数据   */
              title: currentData.sMenuName, /*   菜单名称   */
              route: currentData.sName, /*   路由名称   */
              formId: currentData.sId, /*   窗体编号   */
              sModelsType: currentData.sModelType, /*   窗体类型   */
              key: paneKey, /*   页签标识   */
              sParentId, /*   父页面编号   */
              checkedId, /*   单据主表编号   */
              sFilterOrderBy, /*   列表页排序规则(用于上一条下一条查询)   */
              pages, /*   列表页分页信息(用于上一条下一条查询)   */
              bFilter, /*   列表页查询条件(用于上一条下一条查询)   */
              currentIndex, /*   列表页游标(用于上一条下一条查询)   */
              sListFormmasterId,
            },
          },
        });
      } else { /*   失败   */
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *getUrlData({ payload }, { put, call, select }) {
      const {
        currentIndex, refresh, url, sTabId, sParentId, formRoute, componentName, urlDataType, returnType, checkedId, disabled, copyToData, sFilterOrderBy, bFilter, pageSize, pages, sListFormmasterId, currentData, newKey,
      } = payload;
      const token = yield select(state => state.app.token);
      let dataList = {
        sDataListTabId: sTabId, formRoute, componentName, loading: true,
      };
      yield put({ type: 'setDataList', payload: { dataList } });
      const { data } = yield call(services.getService, token, url);
      let paneType = '';
      if (data.code === 1) {
        let rows;
        if (urlDataType === 'First') {
          rows = data.dataset.rows.length > 0 ? data.dataset.rows[0] : {};
        } else if (urlDataType === 'newPane') {
          rows = data.dataset.rows.length > 0 ? data.dataset.rows[0] : {};
          paneType = 'new';
        } else {
          const rowsCopy = data.dataset.rows;
          rows = rowsCopy;
        }
        dataList = {
          sDataListTabId: sTabId,
          sParentId, /* 父页面ID用于标识新页签是从哪个页面上来的，防止双击出现多个页签 */
          formRoute,
          componentName,
          urlData: rows,
          loading: false,
          disabled,
          paneType,
          checkedId, /* 双击选中的Table中的数据ID */
          returnType,
          copyToData, /* 复制到数据 */
          sFilterOrderBy,
          bFilter,
          pageSize,
          pages,
          sListFormmasterId,
          currentData,
          newKey,
          refresh, /* commonList页面刷新方法 */
          currentIndex,
        };
        yield put({ type: 'setDataList', payload: { dataList } });
        dataList = {};
        yield put({ type: 'setDataList', payload: { dataList } });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *getUrlPostData({ payload }, { put, call, select }) {
      /* 获取表数据 */
      const {
        formRoute, componentName, sTabId, url, value, urlDataType, returnType, sId,
      } = payload;
      let dataList = {
        sDataListTabId: sTabId, formRoute, componentName, loading: true,
      };
      if (returnType !== 'CUD') {
        yield put({ type: 'setDataList', payload: { dataList } });
      }
      const token = yield select(state => state.app.token);
      const { data } = yield call(services.postValueService, token, value, url);
      if (data.code === 1) {
        if (returnType !== undefined && returnType === 'CUD') {
          message.success(data.msg);
          dataList = {
            sDataListTabId: sTabId, formRoute, componentName, returnType, disabled: true, sId,
          };
          yield put({ type: 'setDataList', payload: { dataList } });
        } else {
          const { dataset } = data;
          let dataTotal = {};
          if (data.dataset.rows.length > 0 && data.dataset.rows[0].sumSet !== undefined) {
            dataTotal = data.dataset.rows[0].sumSet;
          }
          let rows;
          if (urlDataType === 'First') {
            rows = data.dataset.rows.length > 0 && data.dataset.rows[0].dataSet.length > 0 ? data.dataset.rows[0].dataSet[0] : {};
          } else {
            rows = data.dataset.rows.length > 0 ? data.dataset.rows[0].dataSet : [];
          }
          // const { rows } = dataset; /* 整合前列表数据 */
          const pages = {
            pageNum: dataset.currentPageNo, /* 当前页 */
            totalCount: dataset.totalCount, /* 总数 */
          }; /* 分页 */
          dataList = {
            sDataListTabId: sTabId, formRoute, componentName, urlData: rows, pages, returnType, loading: false, dataTotal,
          };
          yield put({ type: 'setDataList', payload: { dataList } });
          /* 用以清空数据集屏蔽类似保存后闪显老数据等问题 */
          dataList = {};
          yield put({ type: 'setDataList', payload: { dataList } });
        }
      } else {
        yield put({ type: 'app/throwError', payload: data });
        dataList = {
          sDataListTabId: sTabId, formRoute, componentName, urlData: [], returnType, loading: false, pages: { pageNum: 0, totalCount: 0 }, dataTotal: [{}],
        };
        yield put({ type: 'setDataList', payload: { dataList } });
      }
    },
    *getUrlPostDataSys({ payload }, { put, call, select }) {
      /* 系统参数保存 */
      const {
        formRoute, componentName, sTabId, url, value, returnType, sId,
      } = payload;
      const token = yield select(state => state.app.token);
      const { data } = yield call(services.postValueService, token, value, url);
      if (data.code === 1) {
        message.success(data.msg);
        const dataList = {
          sDataListTabId: sTabId, formRoute, componentName, returnType, disabled: true, sId,
        };
        yield put({ type: 'setDataList', payload: { dataList } });
        const sysData = value.data;
        const systemData = sysData.filter(item => item.sTable === 'syssystemsettings')[0].column;
        const dNetPrice = systemData.filter(item => (item.sName === 'NetPrice'))[0];
        const dNetMoney = systemData.filter(item => (item.sName === 'NetMoney'))[0];
        const decimals = {
          dNetPrice: dNetPrice.sValue !== undefined ? (dNetPrice.sValue) * 1 : 6,
          dNetMoney: dNetMoney.sValue !== undefined ? (dNetMoney.sValue) * 1 : 6,
        };
        yield put({ type: 'app/saveDecimals', payload: decimals });
        yield put({ type: 'app/saveSystemData', payload: systemData });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *getCopyDataId({ payload }, { put, call, select }) {
      const {
        sTabId, formRoute, componentName, url,
      } = payload;
      const token = yield select(state => state.app.token);
      const { data } = yield call(services.getService, token, url);
      if (data.code === 1) {
        const dataList = {
          sDataListTabId: sTabId,
          formRoute,
          componentName,
          newIds: data.dataset.rows,
        };
        yield put({ type: 'setDataList', payload: { dataList } });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
  },
  subscriptions: {},
};

